#pragma once
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>

class Dict
{
    void ReadFile()
    {
        std::ifstream f(_filepath);
        if (!f.is_open())
        {
            std::cerr << "open file failed" << std::endl;
            exit(1);
        }
        std::cout << "open file success" << std::endl;
        std::string line;
        while (std::getline(f, line))
        {
            size_t pos = line.find(":");
            if (pos == std::string::npos)
            {
                continue;
            }
            std::string key = line.substr(0, pos);
            std::string value = line.substr(pos+1);
            std::cout << key << " : " <<  value << std::endl;
             if (value.size() == 0)
            {
                continue;
            }
            _dict[key] = value;
        }
        f.close();
    }
    // 读取配置文件 构建字典

public:
    Dict(const std::string filepath)
        : _filepath(filepath)
    {
        ReadFile();
    }
    // 根据用户输入，给出翻译
    const std::string Translate(const std::string &word)
    {
        std::cout << "word: " << word <<std::endl;
        auto res = _dict.find(word);
        if (res == _dict.end())
        {
             return "Not Found";
        }
        else
        {
            return res->second;
        }
    }
    Dict() {}

private:
    std::string _filepath;
    std::unordered_map<std::string, std::string> _dict;
};